package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.presto.directo.R
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.ActivityRecordListBinding
import com.presto.directo.mvvm.adapter.RecordListAdapter
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*

class RecordListActivity : BaseActivity() {
    override val rootView: View
        get() = binding.root

    private val binding by lazy { ActivityRecordListBinding.inflate(layoutInflater) }
    private val recordListAdapter by lazy { RecordListAdapter(R.layout.item_record_list) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_record_list_page.track(this)
        binding.rvRecord.layoutManager = LinearLayoutManager(this).apply { orientation = RecyclerView.VERTICAL }
        binding.rvRecord.adapter = recordListAdapter
        recordListAdapter.addChildClickViewIds(R.id.tv_more, R.id.btn_repay)
        recordListAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.tv_more -> {
                    recordListAdapter.getItem(position).expand = !recordListAdapter.getItem(position).expand
                    recordListAdapter.notifyItemChanged(position, "expand")
                }
                R.id.btn_repay -> {
                    val intent = Intent(this, RepayActivity::class.java)
                    intent.putExtra("amount", recordListAdapter.getItem(position).remainAmount)
                    intent.putExtra("busiId", recordListAdapter.getItem(position).busiId)
                    startActivity(intent)
                }
            }
        }
        start(onNext = {
            showProgress()
            getApiService().getRecord(getHttpMap().sign(14)).getResultOrNull()?.let {
                recordListAdapter.addData(it)
            }
        }, onComplete = {
            dismissProgress()
        })
    }
}